function [value isterminal direction] = eventfcn(t,y,a)
   


%   a = [  1.3127   -0.0000         0         0         0
%    -0.5558    4.7510   -1.7836   -0.1006         0
%    -0.1756   10.1011    0.5204    7.3356    0.2031
%    -0.4061   10.4827    0.2320   -0.4702    0.5975];

%     direction = -1;
    direction = -1;
    pos = jpos_mat(y);
%     pos = jpos_mat([0;0;y]);
    isterminal = 0;
    value = 0;
    
  if (pos(1,1) < pos(1,end))
        
   value = h_sca(y);
   % value = -tau(y,a)+0.5208;
    isterminal = 1;
%         end
    end
    
%      y5=y(1)+y(2)+y(3)-y(4)+pi;
%     value = (cos(y(1)) - cos(y(2))+0.002);
%     isterminal = 1; % stop when scuff

%      if y(2) < -0.25   % ----------
% % % %     if y(8)>0
%          isterminal = 0;
%      else
%          isterminal = 1;
%      end

%     if ((-pi/1)<y5 && y5<0)  % nonstance leg  is negative just before the impact
%         if ((pi/2)>y(1) && y(1)>0) % stance leg is positive
%             isterminal = 1;% make sure this is a step and not a scuff
%             
%         else
%             isterminal=0;
%         end
%     else
%         isterminal=0;
% 
%     end


end
